package com.leps.nq.tx.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.tx.entity.PumpEffectivePoint;
import com.leps.nq.tx.service.IPumpEffectivePointService;
import com.leps.nq.tx.vo.PumpPointVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 泵实际点位表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/pump-effective-point")
@Api(tags = "泵点位管理")
public class PumpEffectivePointController {
    @Autowired
    IPumpEffectivePointService pumpEffectivePointService;

    @ApiOperation("根据泵id查询泵实际点位信息")
    @GetMapping
    public R<List<PumpEffectivePoint>> getByPumpId(int pumpId) {
        return RBuilder.ok(this.pumpEffectivePointService.lambdaQuery().eq(PumpEffectivePoint::getPumpId, pumpId).orderByAsc(PumpEffectivePoint::getDataAddress).list());
    }

    @ApiOperation("设置泵点位")
    @PostMapping
    @Transactional
    @Log(title = "泵点位", businessType = BusinessType.OTHER)
    public R setEffectivePoint(@RequestBody List<PumpEffectivePoint> pumpEffectivePoints) {
        if(ObjectUtil.isEmpty(pumpEffectivePoints)) return RBuilder.error("不能提交空数据");
        int pumpId = pumpEffectivePoints.get(0).getPumpId();
        this.pumpEffectivePointService.remove(new QueryWrapper<PumpEffectivePoint>().eq("pump_id", pumpId));
        this.pumpEffectivePointService.saveBatch(pumpEffectivePoints);
        return RBuilder.ok();
    }

    @GetMapping("/plcinfo")
    @ApiOperation("根据泵ID查询plc信息")
    public R<PumpPointVo> plcInfo(int pumpId) {
        PumpPointVo vo = this.pumpEffectivePointService.selectPumpPointVoByPumpId(pumpId);
        return RBuilder.ok(vo);
    }
}
